/* 
 * Created on 2006-1-5
 * Last modified on 2007-5-22
 * www.YeQiangWei.com
 */
package com.yeqiangwei.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;

/**
 * @author YeQiangWei by 我容易么我
 * Powered by YeQiangWei.com
 */
public class XmlOperate {
	
	private static final Logger logger = Logger.getLogger(XmlOperate.class.getName());
	
	private Document document = null;
	
	private String xmlPath = "";

	
	public XmlOperate(){
		
	}
	
	public XmlOperate(String xmlpath){
		try{
			this.xmlPath = xmlpath;
			SAXBuilder builder = new SAXBuilder();
			this.document = (builder.build(xmlpath));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	public XmlOperate(URL url){
		try{
			SAXBuilder builder = new SAXBuilder();
			this.document = (builder.build(url));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	public XmlOperate(InputStream stream){
		try{
			SAXBuilder builder = new SAXBuilder();
			this.document = (builder.build(stream));
		}catch(Exception e){
			logger.error(e.toString());
		}
	}
	
	public List getRootElementChlildren(){
		return this.document.getRootElement().getChildren();
	}
	
	
	public Element getElement(String child){
		Element ele = null;
		try{
			ele = (Element)this.document.getRootElement().getChild(child);
		}catch(Exception e){
			logger.error(e.toString());
		}
		return ele;
	}

	/*
	public String getChildren(Element ele, String name){
		String s = "";
		Element ele2;
		try{
			s = ele.getChildText(name);
			List list = ele.getChildren();
			for(int i=0; i<list.size(); i++){
				ele2 = (Element) list.get(i);
				System.out.println(getChildText(ele2,"file"));
			}
		}catch(Exception e){
			s = "";
			logger.error(e.toString());
		}
		return s;
	}
	*/
	
	public String getChildText(Element ele, String c){
		if(ele==null){
			return null;
		}else{
			String s = null;
			try{
				s = ele.getChildText(c);
			}catch(Exception e){
				logger.error(e.toString());
			}
			return s;
		}
	}
	
	public void setChildText(Element ele, String c, String s){
		if(ele==null){
			return ;
		}
		Element eleU = ele.getChild(c);
		if(eleU==null){
			eleU = this.createElement(ele,c);
		}		
		eleU.setText(s);
		org.jdom.output.XMLOutputter xml= new org.jdom.output.XMLOutputter();
		try {
			Format format=Format.getPrettyFormat(); //格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
	}
	
	public void setCDATA(Element ele, String child, String str)
	{
		CDATA cdata = new CDATA(str);
		Element eleU = ele.getChild(child);
		if(eleU==null){
			eleU = this.createElement(ele,child);
		}
		eleU.setContent(cdata);
		org.jdom.output.XMLOutputter xml= new org.jdom.output.XMLOutputter();
		try {
			Format format=Format.getPrettyFormat(); //格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
	}
	
	public String getCDATA(Element ele, String child){
		return getChildText(ele,child);
	}
	
	public Element createElement(Element ele, String child){
		Element eleU = new Element(child);
		ele.addContent(eleU);
		org.jdom.output.XMLOutputter xml= new org.jdom.output.XMLOutputter();
		try {
			Format format=Format.getPrettyFormat(); //格式化文档
			xml.setFormat(format);
			xml.output(this.document, new FileOutputStream(xmlPath));
		} catch (FileNotFoundException e) {
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error(e.toString());
		}
		return eleU;
	}
	
	/*
	public static void main(String args[]){
		XmlOperate xo = new XmlOperate("E:\\MYJAVA\\myweb\\WEB-INF\\classes\\yeqiangweiclub.cfg.xml");
		//String s = xo.getChildText(xo.getElement("club-cfg"),"clubName");
		xo.setCDATA(xo.getElement("club-cfg"),"clubName2","ddddddd");
		//System.out.println(s);
	}
	*/

	public String getXmlPath() {
		return xmlPath;
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}
}
